/*
*  Copyright (c) 2018. 玺得（深圳）科技有限公司.保留所有权利. http://www.pi-top.com/ 玺得（深圳）科技有限公司保留所有代码著作权.如有任何疑问请访问官方网站与我们联系. 代码只针对特定需求定制编码，不得在未经允许或授权的情况下对外传播扩散.恶意传播者，法律后果自行承担. 本代码仅用于pps-cloud系统.
*/
package com.pitop.order.ctrl;


import com.alibaba.fastjson.JSON;
import com.pitop.core.entity.BeanRet;
import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.tools.StringTools;
import com.pitop.core.tools.redis.RedisUtils;
import com.pitop.order.VO.OrderDetailVO;
import com.pitop.order.entity.Order;
import com.pitop.order.entity.OrderDetail;
import com.pitop.order.service.OrderDetailSVImpl;
import com.pitop.order.service.OrderSVImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.List;

/**
 * 订单详情 控制器
 *
 * @author berton
 */
@RestController
@RequestMapping("/orderDetail")
@Slf4j
@Api(value = "订单详情控制器", description = "订单详情控制器")
public class OrderDetailCtrl {

    @Resource
    protected RedisUtils redisUtils;
    @Autowired
    private OrderDetailSVImpl orderDetailSVImpl;
    @Autowired
    private OrderSVImpl orderSVImpl;


    /**
     * 查询OrderDetail一个详情信息
     *
     * @param id
     * @return BeanRet
     */
    @ApiOperation(value = "查询OrderDetail一个详情信息", notes = "查询OrderDetail一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "query")
    })
    @GetMapping(value = "/load")
    @ResponseBody
    public BeanRet load(Long id) {
        if (id == null) {
            return null;
        }
        OrderDetail orderDetail = orderDetailSVImpl.load(id);
        log.info(JSON.toJSONString(orderDetail));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, orderDetail);
    }


    /**
     * 根据条件id查询OrderDetail一个详情信息
     *
     * @param id
     * @return BeanRet
     */
    @ApiOperation(value = "查询OrderDetail一个详情信息", notes = "查询OrderDetail一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "path")
    })
    @GetMapping(value = "/load/id/{id}")
    @ResponseBody
    public BeanRet loadById(@PathVariable Long id) {
        if (id == null || id == 0) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        OrderDetail orderDetail = orderDetailSVImpl.loadById(id);
        log.info(JSON.toJSONString(orderDetail));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, orderDetail);
    }


    /**
     * 查询OrderDetail信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询OrderDetail信息集合", notes = "查询OrderDetail信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),

            @ApiImplicitParam(name = "createTimeBegin", value = "创建时间", paramType = "query"),
            @ApiImplicitParam(name = "createTimeEnd", value = "创建时间", paramType = "query")
    })
    @GetMapping(value = "/list")
    @ResponseBody
    public BeanRet list(@ApiIgnore OrderDetail orderDetail, @ApiIgnore Page<OrderDetail> page) {
        if (page == null) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        List<OrderDetail> orderDetails = orderDetailSVImpl.list(orderDetail, page.getCurPage(), page.getPageSize());
        int total = orderDetailSVImpl.count(orderDetail);
        page.setTotalRow(total);
        page.setVoList(orderDetails);
        log.info(JSON.toJSONString(page));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, "", page);
    }


    /**
     * 查询OrderDetail信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询OrderDetail信息集合", notes = "查询OrderDetail信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "id", value = "", paramType = "query")
    })
    @GetMapping(value = "/list/by")
    @ResponseBody
    public BeanRet listByPk(Long id, @ApiIgnore Page<OrderDetail> page) {
        if (page == null) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        List<OrderDetail> orderDetails = orderDetailSVImpl.list(id, page.getCurPage(), page.getPageSize());
        int total = orderDetailSVImpl.count(id);
        page.setTotalRow(total);
        page.setVoList(orderDetails);
        log.info(JSON.toJSONString(page));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, "", page);
    }

    /**
     * 创建OrderDetail
     *
     * @return BeanRet
     */
    @ApiOperation(value = "创建OrderDetail", notes = "创建OrderDetail")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "orderCode", value = "订单编码", paramType = "query"),
            @ApiImplicitParam(name = "boxSn", value = "箱体SN码", paramType = "query"),
            @ApiImplicitParam(name = "itemSn", value = "产品SN", paramType = "query"),
            @ApiImplicitParam(name = "boxNum", value = "箱号", paramType = "query"),
            @ApiImplicitParam(name = "itemNum", value = "装箱数量", paramType = "query"),
            @ApiImplicitParam(name = "linkman", value = "联系人", paramType = "query"),
            @ApiImplicitParam(name = "phone", value = "电话", paramType = "query"),
            @ApiImplicitParam(name = "email", value = "邮箱", paramType = "query"),
            @ApiImplicitParam(name = "address", value = "地址", paramType = "query"),
            @ApiImplicitParam(name = "createTime", value = "创建时间", paramType = "query")
    })
    @PostMapping("/build")
    @ResponseBody
    public BeanRet build(@ApiIgnore OrderDetail orderDetail) {
        if (StringUtils.isEmpty(orderDetail.getOrderCode())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(orderDetail.getBoxSn())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(orderDetail.getItemSn())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(orderDetail.getBoxNum())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(orderDetail.getItemNum())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }

        orderDetailSVImpl.save(orderDetail);
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, orderDetail);
    }


    /**
     * 修改OrderDetail
     *
     * @return BeanRet
     */
    @ApiOperation(value = "修改OrderDetail", notes = "修改OrderDetail")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "orderCode", value = "订单编码", paramType = "query"),
            @ApiImplicitParam(name = "boxSn", value = "箱体SN码", paramType = "query"),
            @ApiImplicitParam(name = "itemSn", value = "产品SN", paramType = "query"),
            @ApiImplicitParam(name = "boxNum", value = "箱号", paramType = "query"),
            @ApiImplicitParam(name = "itemNum", value = "装箱数量", paramType = "query"),
            @ApiImplicitParam(name = "linkman", value = "联系人", paramType = "query"),
            @ApiImplicitParam(name = "phone", value = "电话", paramType = "query"),
            @ApiImplicitParam(name = "email", value = "邮箱", paramType = "query"),
            @ApiImplicitParam(name = "address", value = "地址", paramType = "query"),
            @ApiImplicitParam(name = "createTime", value = "创建时间", paramType = "query")
    })
    @PutMapping("/modify")
    @ResponseBody
    public BeanRet modify(@ApiIgnore OrderDetail orderDetail) {
        if (StringUtils.isEmpty(orderDetail.getOrderCode())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(orderDetail.getBoxSn())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(orderDetail.getItemSn())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(orderDetail.getBoxNum())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(orderDetail.getItemNum())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        orderDetailSVImpl.modify(orderDetail);
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, orderDetail);
    }

    /**
     * 根据主键删除OrderDetail
     *
     * @return BeanRet
     */
    @ApiOperation(value = "删除OrderDetail", notes = "删除OrderDetail")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query")
    })
    @DeleteMapping("/delete")
    @ResponseBody
    public BeanRet delete(Long id) {
        if (id != null) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }

        orderDetailSVImpl.delete(id);
        return BeanRet.create(true, "删除OrderDetail成功");
    }

    /**
     * 根据订单编号查询绑定箱体
     */
    @ApiOperation(value = "根据订单编号查询绑定箱体", notes = "根据订单编号查询绑定箱体")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "orderCode", value = "订单编码", paramType = "query", required = true),
    })
    @GetMapping(value = "/listBox")
    @ResponseBody
    public BeanRet listBox(String orderCode, @ApiIgnore Page<OrderDetailVO> page) {
        if (page == null) {
            return BeanRet.create(false, "分页对象不能为空");
        }
        List<OrderDetail> orderDetails = orderDetailSVImpl.listBox(orderCode, page.getCurPage(), page.getPageSize());
        List<OrderDetailVO> orderDetailVOS = StringTools.convertList(orderDetails, OrderDetailVO.class);
        List<OrderDetail> orderDetailsList;
        OrderDetail orderDetail;
        if (orderDetailVOS != null && orderDetailVOS.size() > 0) {
            for (OrderDetailVO orderDetailVO : orderDetailVOS) {
                orderDetail = new OrderDetail();
                orderDetail.setBoxSn(orderDetailVO.getBoxSn());
                orderDetailsList = orderDetailSVImpl.list(orderDetail, 0, 50);
                orderDetailVO.setOrderDetails(orderDetailsList);
            }
        }
        page.setVoList(orderDetailVOS);
        Order order = orderSVImpl.loadByCode(orderCode);
        page.setTotalRow(order.getBindItemBoxNum());
        return BeanRet.create(true, "查询成功", page);

    }


}
